package com.liugh.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.liugh.config.ResponseModel;
import com.liugh.config.jackson.JsonResultProcessor;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;


public class BaseRestController {
    private static org.slf4j.Logger Logger = LoggerFactory.getLogger(BaseRestController.class);

    public static  <T> ResponseModel<T> fillSuccessData(T data) {
        Logger.info("返回数据为：{}", data);
        return new ResponseModel<T>(HttpStatus.OK.value(),data, HttpStatus.OK.getReasonPhrase(), HttpStatus.OK.getReasonPhrase());
    }

    public <String> ResponseModel<String> fillSuccessData() {
        return new ResponseModel<String>(HttpStatus.OK.value(),null, HttpStatus.OK.getReasonPhrase(), HttpStatus.OK.getReasonPhrase());
    }

    public <T> ResponseModel<T> fillFailData(T data) {
        Logger.info("request失败，返回数据为：{}", data);
        return new ResponseModel<T>(HttpStatus.BAD_REQUEST.value(),data, HttpStatus.BAD_REQUEST.getReasonPhrase(), HttpStatus.BAD_REQUEST.getReasonPhrase());
    }

    public <String> ResponseModel<String> fillFailData() {
        return new ResponseModel<String>(HttpStatus.BAD_REQUEST.value(),null, HttpStatus.BAD_REQUEST.getReasonPhrase(), HttpStatus.BAD_REQUEST.getReasonPhrase());
    }

    public  <T> ResponseModel<String> fillSuccessData(String data) {
        Logger.info("返回数据为：{}", data);
        return new ResponseModel<String>(HttpStatus.OK.value(),data, HttpStatus.OK.getReasonPhrase(), HttpStatus.OK.getReasonPhrase());
    }

    public static  <T> ResponseModel<String> fillFailData(String data) {
        Logger.info("request失败，返回数据为：{}", data);
        return new ResponseModel<String>(HttpStatus.BAD_REQUEST.value(),data, HttpStatus.BAD_REQUEST.getReasonPhrase(), HttpStatus.BAD_REQUEST.getReasonPhrase());
    }

    /**
     * 专门处理page对象
     * @param data
     * @param <T>
     * @return
     */
    public <T> ResponseModel<T> fillSuccessDataPage(T data) {
        Logger.info("返回数据为：{}", data);

        return JsonResultProcessor.processJsonResult(new ResponseModel<T>(HttpStatus.OK.value(), data, HttpStatus.OK.getReasonPhrase(), HttpStatus.OK.getReasonPhrase()),
                JsonResultProcessor.Rules
                        .start()
                        .onClass(Page.class)
                        .addExcludes("offset")
                        .addExcludes("limit")
                        .addExcludes("searchCount")
                        .addExcludes("openSort")
                        .addExcludes("ascs")
                        .addExcludes("descs")
                        .addExcludes("orderByField")
                        .addExcludes("condition")
                        .addExcludes("asc")
                        .done());
    }

    public static  <T> ResponseModel<T> validationFailure(T data) {
        return new ResponseModel<>(HttpStatus.UNAUTHORIZED.value(),data,HttpStatus.UNAUTHORIZED.getReasonPhrase(),HttpStatus.UNAUTHORIZED.getReasonPhrase());
    }

}
